/*
 * Copyright (c) 2007 Compl Yue Still, All Rights Reserved.
 */

/**
 * 
 */
package hbi.task.java;

import hbi.domain.java.JavaGateway;
import hbi.task.TaskAgent;

import java.lang.annotation.Annotation;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class TaskAgentLifeScript
{

    private final AtomicInteger retryCount = new AtomicInteger();

    public int countRetry()
    {
        return retryCount.incrementAndGet();
    }

    final Class<?> agentClass;

    final List<Annotation> headers;

    final LinkedList<AgentMethodInvocation> invocations;

    public TaskAgentLifeScript(Class<?> agentClass, List<Annotation> headers,
        LinkedList<AgentMethodInvocation> invocations)
    {
        this.agentClass = agentClass;
        this.headers = headers;
        this.invocations = invocations;
    }

    @SuppressWarnings("unchecked")
    public void liveUp(JavaGateway<?, ?> gateway) throws Exception
    {
        TaskAgent agentObject = (TaskAgent) this.agentClass.newInstance();
        agentObject.arrived(gateway);
        for (AgentMethodInvocation invocation : this.invocations)
            invocation.method.invoke(agentObject, invocation.parameters);
        agentObject.finishTask(gateway);
    }

}
